<template>
  <el-form-item>
    <span v-if="this.usedfor != 2" style="color: #888;">采购渠道：</span>
    <el-select v-if="this.usedfor != 2" placeholder="请选择采购渠道" v-model="channel" filterable @change="selectChanged(1)" style="width:120px;">
      <el-option v-for="item in channelList" :key="item.id" :label="item.name" :value="item.id"></el-option>
    </el-select>
    <el-select v-if="channel==1" placeholder="请选择供应商" v-model="supplier_id" filterable clearable @change="selectChanged(2)" style="width:150px;">
      <el-option v-for="item in supplier_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
    </el-select>
    <el-select v-if="channel==1" placeholder="请选择对接人" v-model="docking_id" filterable clearable @change="selectChanged(3)" style="width:150px;">
      <el-option v-for="item in docking_list" :key="item.id" :label="item.nick" :value="item.id">
      </el-option>
    </el-select>
  </el-form-item>
</template>
<script>
import { getSupplier, getDocking } from "@/api/common/gift_purchase.js";
export default {
  name: "DjGiftChannelSelect",
  props: {
    data: {
      type: Object,
      default() {
        return {};
      }
    },
    from: {
      type: Number,
      default: 0
      // 1：供应商，2：中央仓
    },
    usedfor: {
      type: Number,
      default: 0
      // 1:运管系统采购下单，2：采购平台总部下单，3：采购平台总部统配
    }
  },
  data() {
    return {
      channel: this.usedfor == 2 ? 1 : this.from, // 1：供应商，2：中央仓
      channelList: [{ id: 2, name: "中央仓" }, { id: 1, name: "供应商" }],
      supplier_id: "", // 供应商id
      docking_id: "", // 对接人
      supplier_list: [],
      docking_list: []
    };
  },
  created() {
    getSupplier({}).then(response => {
      if (!response) response = [];
      this.supplier_list = response;
    });
    getDocking().then(response => {
      if (!response) response = [];
      this.docking_list = response;
    });
  },
  methods: {
    selectChanged(level) {
      if (level == 1) {
        this.supplier_id = "";
        this.docking_id = "";
        this.$emit("channel", this.channel);
        return;
      }
      let val = {
        from: this.channel,
        supplier_id: this.supplier_id,
        docking_id: this.docking_id
      };
      this.$emit("change", val);
    }
  }
};
</script>